<template>
  <div>
    <!-- 注册 -->
    <!-- 导航栏 -->
    <van-nav-bar
      :title="$route.name"
      @click-left="$router.back()"
      left-text="返回"
      left-arrow
      right-text="注册"
      @click-right="$router.push('/register')"
    />
    <!-- 输入表单 -->
    <van-form @submit.13="login">
      <van-field
        v-model="userlist.phone"
        name="手机号"
        label="手机号"
        placeholder="请输入手机号"
        :rules="[{ required: true, message: '请填写手机号' }]"
      />
      <van-field
        v-model="userlist.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { Toast } from "vant";
import { login } from "../request/api";
export default {
  data() {
    return {
      userlist: {
        phone: "",
        password: "",
      },
    };
  },
  methods: {
    //封装一个登录事件
    login() {
      login(this.userlist).then((res) => {
        console.log(res, "登录");
        if (res.code == 200) {
          //放在跳转之前
          this.$store.dispatch('changeUser',res.list)
          Toast.success(res.msg);
          this.$router.push("/home");
        } else {
          Toast.fail(res.msg);
        }
      });
    },
  },
};
</script>

<style scoped>
</style>